package com.xiaohutech.tm.home.ui.fm

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.viewbinding.ViewBinding
import com.xhtech.base.utils.kts.rmb
import com.xiaohutech.tm.home.data.bean.GoodsListBean
import com.xiaohutech.tm.home.databinding.HomeFragmentBinding
import com.xiaohutech.tm.home.databinding.HomeItemLayoutBinding
import com.xiaohutech.tm.home.ui.vm.HomeViewModule
import com.yxh.lib_base.base.BaseFragment
import com.yxh.lib_base.ext.BaseViewBindingAdapter
import com.yxh.lib_base.ext.VBViewHolder
import com.yxh.lib_base.ext.dp2px
import com.yxh.lib_base.ext.lazyNone
import com.yxh.lib_base.ext.recycler.addItemDecorationSpaces
import com.yxh.lib_base.ext.recycler.initRcyLinear
import com.yxh.lib_base.ext.view.buildSpannableString
import com.yxh.lib_base.ext.viewbinding.binding
import com.yxh.lib_base.ext.viewbinding.viewBinding
import com.yxh.lib_base.util.image.load

/**
 * @author yuanxh
 * @date :2024/2/4 21:29
 * @description:
 */
class HomeFragment : BaseFragment() {
    override val mBinding by viewBinding<HomeFragmentBinding>()
    private val mViewModule by activityViewModels<HomeViewModule>()
    private val mAdapter by lazyNone { GoodsAdapter() }

    override fun onCreated(savedInstanceState: Bundle?) {
        initStatusBar {
            titleBar(mBinding.layoutTop)
        }
        mViewModule.loadData()
        mBinding.recyclerView.initRcyLinear(mAdapter)
            .addItemDecorationSpaces(12f.dp2px(), 12f.dp2px())
        mBinding.tvGoodsTitle.buildSpannableString {
            addText("精选推送 · "){
                setBold()
                setProportion(1.2f)
            }
            addText("附近摊位(2)")
        }
        mViewModule.data.observe(this){
            mAdapter.setList(it)
        }
    }

    inner class GoodsAdapter : BaseViewBindingAdapter<HomeItemLayoutBinding, GoodsListBean>() {
        override fun convert(holder: VBViewHolder<HomeItemLayoutBinding>, item: GoodsListBean) {
            with(holder.vb) {
                tvName.text = item.name
                tvStatus.buildSpannableString {
                    addText("${rmb()}${item.price}")
                    addText(" · ")
                    addText(item.status)
                }
                tvUser.buildSpannableString {
                    addText(item.userName)
                    addText("  ")
                    addText(item.distance)
                }
                ivFace.load(item.face){
                    radius(8f)
                }
            }

        }
    }
}